#include "project.h"

char LED_states[4];
char FAN_states[4];
char BEEP_states[4];

int cgiMain(int argc, const char *argv[])
{
	int ret = 0;
	//公共空间获取环境数据 存入仓库环境数据
	cgiFormString("LED",LED_states,4);//获取网页form表单 led设置	
	cgiFormString("FAN",FAN_states,4);//获取网页form表单 fan设置
	cgiFormString("BEEP",BEEP_states,4);//获取网页form表单	beep设置

	memset(&msg,0,sizeof(msg_t));
	
	/*检查设备设置值，并赋值到消息结构中*/
	ret = strncmp("ON",LED_states,4);
	if(0==ret){
		msg.dev_ctrl_msg.led = 1;
	}else{
		msg.dev_ctrl_msg.led = 0;
	}
	
	ret = strncmp("ON",FAN_states,4);
	if(0==ret){
		msg.dev_ctrl_msg.fan = 1;		
	}else{	
		msg.dev_ctrl_msg.fan = 0;
	}
	ret = strncmp("ON",BEEP_states,4);
	if(0==ret){
		msg.dev_ctrl_msg.beep = 1;
	}else{
		msg.dev_ctrl_msg.beep = 0;
	}
	
	/*创建消息队列*/
	key_t key = 0; 
	if((key = ftok("/home/linux",'a')) == -1) 
	{ 
		perror("ftok"); 
		return -1; 
	}

	msqid = msgget(key,IPC_CREAT|0666); 
	if(msqid == -1) { 
		perror("msgget"); 
		return -1; 
	}

	/*初始化消息类型 及仓库号*/
	msg.mtype = 4;
	msg.store_num = 1;

	/*向数据处理进程发送设备控制消息*/
	msgsnd(msqid,&msg,sizeof(msg_t)-sizeof(long),0);	

	/*输出网页*/
	printf("Content-type: text/html;charset=\"UTF-8\"\n\n");
	printf("<html>");	
	printf("<body bgcolor=\"green\">\n");

	printf("<script>window.location.href = '/one_store.html';</script>\n");

	printf("</body>\n");
	printf("</html>\n");

	return 0;
}
